package com.zijidelu.luframework.starter.scanner.impl;

import com.zijidelu.luframework.base.TypeCaster;
import com.zijidelu.luframework.infrastructure.factory.ConverterVault;
import com.zijidelu.luframework.utils.cache.vault.Vault;
import com.zijidelu.luframework.utils.collection.stream.StreamUtil;
import com.zijidelu.luframework.utils.spring.beans.generic.SpringBeanTypeUtil;
import com.zijidelu.luframework.infrastructure.factory.AggregateVault;
import com.zijidelu.luframework.infrastructure.converter.AggregateConverter;
import com.zijidelu.luframework.starter.scanner.AbstractAutoScanner;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * 聚合转换器扫描器。
 *
 * @author ZIJIDELU
 * @datetime 2025/10/20 22:03
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class AggregateConverterScanner extends AbstractAutoScanner {
    @Setter(onMethod_ = {@Autowired})
    private AggregateVault aggregateVault;
    @Setter(onMethod_ = {@Autowired})
    private ConverterVault converterVault;

    @Override
    public Class<? extends AbstractAutoScanner> dependOn() {
        return DataConverterScanner.class;
    }

    @Override
    public void start() {
        // 按泛型参数组合去重
        final Set<String> processedTypePairs = new HashSet<>();

        // PDConvert
        StreamUtil.smartStream(getSpringDynamicBeanRegisterUtil().getBeansOfType(AggregateConverter.class).values())
                .forEach((bean) -> {
                    // 解析泛型参数
                    final ResolvableType[] resolvableTypes = SpringBeanTypeUtil.resolvableTargetClassTypes(bean, AggregateConverter.class);

                    if (resolvableTypes.length < 3) {
                        // 跳过泛型信息不完整的
                        return;
                    }

                    final Class<?> poClass = resolvableTypes[0].resolve();
                    final Class<?> dataModelClass = resolvableTypes[1].resolve();
                    final Class<?> entityClass = resolvableTypes[2].resolve();
                    if (poClass == null || dataModelClass == null || entityClass == null) {
                        // 跳过无法解析泛型的
                        return;
                    }
                    final String typeKey = Vault.calcItemKeyWithClass(poClass, dataModelClass, entityClass);
                    // 跳过已处理的类型对
                    if (processedTypePairs.contains(typeKey)) {
                        return;
                    }

                    // 注册聚合转换器
                    converterVault.registerAggregateConverter(entityClass, TypeCaster.cast(bean));

                    // 注册聚合保险库
                    aggregateVault.registerEntityRelationWithChildNoCheck(entityClass);

                    // 标记为已处理
                    processedTypePairs.add(typeKey);

                });

    }


}
